package com.routeburn.story;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.PieChart;
import com.googlecode.charts4j.Slice;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
//	User user;
	  Connection con =null;
	  String drivers="org.postgresql.Driver";
	  String url="jdbc:postgresql:usermgr";
	  String username="postgres";
	  String password="dba";
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
//		user = new User();
//		user.setBirthday(new Date());
//		String userDate = dateFormat.format(user.getBirthday() );
//		model.addAttribute("userBirthday", userDate);
		
		logger.info("Sex is " +Gender.valueOf(1).toString());
		dbTest();
		
		return "home";
	}
	
	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String hello(Model model) {
		logger.info("Welcome hello!");
		
		
		return "hello";
	}
	
	@RequestMapping(value = "/googlechart", method = RequestMethod.GET)
	public String googlechart(Model model) {
		logger.info("Welcome googlechart!");
		Slice s1 = Slice.newSlice(15, Color.newColor("CACACA"), "Mac", "Mac");
		Slice s2 = Slice.newSlice(50, Color.newColor("DF7417"), "Window", "Window");
		Slice s3 = Slice.newSlice(25, Color.newColor("951800"), "Linux", "Linux");
		Slice s4 = Slice.newSlice(10, Color.newColor("01A1DB"), "Others", "Others");
		
		PieChart pieChart = GCharts.newPieChart(s1,s2,s3,s4);
		pieChart.setTitle("Google Pie Chart", Color.BLACK, 15);
		pieChart.setSize(720, 360);
		pieChart.setThreeD(true);
		model.addAttribute("pieChart", pieChart.toURLString());
		return "googlechart";
	}
	
	
	private void dbTest(){
		String sql = "select * from testdb";
		  //JDBC프로그래밍 절차
		  try{
		   //1. JDBC드라이버 로딩
		   Class.forName(drivers);
		   System.out.println("드라이버로딩성공");
		   
		   //2. 커넥션 얻기
		   con =DriverManager.getConnection(url, username, password);
		   
		   System.out.println("연결성공");
		   
		   //3. Statement객체생성 - SQL전송
		   Statement stmt = con.createStatement();
		   stmt.execute(sql);
		   ResultSet rs = stmt.executeQuery(sql);
		   rs.next();
		   int key = rs.getInt("key");
		   String id = rs.getString("id");
		   String pw = rs.getString("password");
		   String name = rs.getString("name") ;
		   System.out.println(key);
		   System.out.println(id);
		   System.out.println(pw);
		   System.out.println(name);
		   
		   rs.close();
		   stmt.close();
		  }catch(ClassNotFoundException e){
			   System.out.println("ClassNotFoundException");
			   e.printStackTrace();
			  }catch(SQLException ee){
			   System.out.println("SQLException");
			   ee.printStackTrace();
			  }finally{
			   if(con!=null){
			    try{
			     con.close();
			    }catch(SQLException eee){
			    }
			   }
			 }
	
	}
	
	public enum Gender {
		MALE(1), FEMALE(2);

		private final int value;

		Gender(int value) {
			this.value = value;
		}
		public int getValue() {
			return value;
		}
		public static Gender valueOf(int value) {
			switch(value) {
				case 1: return Gender.MALE;
				case 2: return Gender.FEMALE;
				default: throw new AssertionError("Unknown gender: " + value);
			}
		}
	}
	
}
